package com.itc.system.controller;

import com.itc.common.core.domain.R;
import com.itc.common.core.validate.Add;
import com.itc.common.core.validate.Update;
import com.itc.common.security.utils.SecurityUtils;
import com.itc.system.api.domain.SysMenu;
import com.itc.system.domain.vo.RouterVo;
import com.itc.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 菜单信息
 * 
 * @author itc
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单管理")
public class SysMenuController
{
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 添加菜单
     * @param sysMenu
     * @return
     */
    @ApiOperation(value = "添加菜单")
    @PostMapping("/add")
    public R addSysMenu( @RequestBody @Validated(Add.class) SysMenu sysMenu){
        return sysMenuService.addMenu(sysMenu);
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/delete/{menuId}")
    public R removeSysMenu( @PathVariable(value = "menuId") Long menuId){
        return R.ok(sysMenuService.removeMenu(menuId));
    }

    /**
     * 修改菜单
     * @param sysMenu
     * @return
     */
    @ApiOperation(value = "修改菜单")
    @PostMapping("/update")
    public R updateSysMenu(@Validated(Update.class) @RequestBody SysMenu sysMenu){
        return sysMenuService.updateMenu(sysMenu);
    }

    /**
     * 获取菜单下拉树列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "获取菜单下拉树列表")
    public R<List<SysMenu>> treeSelect(@RequestBody SysMenu menu)
    {
        List<SysMenu> menus = sysMenuService.selectMenuList(menu, SecurityUtils.getUserId());
        return R.ok(sysMenuService.buildMenuAllTree(menus));
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("/getRouters")
    public R<List<RouterVo>> getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = sysMenuService.selectMenuTreeByUserId(userId);
        return R.ok(sysMenuService.buildMenus(menus));
    }

}